#include "IThread.h"

IThread::IThread(void)
{
}

IThread::~IThread(void)
{
}

void IThread::run(HANDLE* pHandle, DWORD* pIdThread)
{
	if (pHandle)
	{
		*pHandle = CreateThread(NULL, 0, threadBegin, this, 0, pIdThread);
	}
	else
	{
		CreateThread(NULL, 0, threadBegin, this, 0, pIdThread);
	}
}

DWORD WINAPI IThread::threadBegin( void* pInstance )
{
	IThread* pThread = (IThread*)pInstance;

	pThread->threadInit();
	pThread->threadMessageLoop();
	pThread->threadEnd();

	return 0;
}

void IThread::threadMessageLoop()
{
	MSG msg;
	ZeroMemory(&msg, sizeof(MSG));

	while (!g_gameOver)
	{
		if (PeekMessage(&msg, (HWND)-1, 0, 0, PM_REMOVE))
		{
			threadHandleMessages(msg.message, msg.wParam, msg.lParam);
		}
		else
		{
			threadProceed();
		}

	}
}
